import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import * as services from "services/Community";
import { addBuilder } from "../dict";

//科室列表
export const doGetDepList = createAsyncThunk<any>(
  "knowledge/doGetDepList",
  async () => {
    const res = services.doGetDepList();
    return res;
  }
);
// 列表
export const communityList = createAsyncThunk<any, number>(
  "knowledge/communityList",
  async (id) => {
    const res = services.findSickCircleList(id);
    return res;
  }
);

// 详情
// 不生效
export const dataDetail = createAsyncThunk<any, number>(
  "knowledge/dataDetail",
  async (id) => {
    const res = services.findSickCircleInfo(id);
    return res;
  }
);

const initialState = {
  knowledge: {},
  depList: [],
  commDetail: [],
};

const Knowledge = createSlice({
  name: "knowledge",
  initialState,
  reducers: {},
  extraReducers: (buildr) => {
    // 病友圈列表展示
    addBuilder(buildr, communityList, (state, action) => {
      const id = action.meta.arg;
      const { result } = action.payload;
      if (result.length) state.knowledge[id] = result;
    });
    // 病友圈详情
    addBuilder(buildr, dataDetail, (state, action) => {
      console.log(action);
      // const { result } = action.payload;
      // state.commDetail = result;
    });
    // 病友圈科室
    addBuilder(buildr, doGetDepList, (state, action) => {
      const { result } = action.payload || {};
      if (result) {
        state.depList = result;
      }
    });
  },
});

export default Knowledge.reducer;

// 列表展示
export const knowLedgeSelect = (globalState: any) => {
  return globalState.knowledge.knowledge;
};
//列表详情
// export const dataDetailSelect = (globalState: any) => {
//   return globalState.knowledge.commDetail;
// };

//科室列表
export const depListSelect = (globalState: any) => {
  return globalState.knowledge.depList;
};
